//index.js
//获取应用实例
const app = getApp()
 
Page({
  data: {
    username: '',
    password: '',
    password1: ''
  },
  onShow: function () {
    // 生命周期函数--监听页面显示
    // wx.hideTabBar({})
  },
  onLoad: function () {
  },
  // 获取输入账号 
  usernameInput: function (e) {
    this.setData({
      username: e.detail.value
    })
  },
 
  // 获取输入密码 
  passwordInput: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
 
  // 获取重复输入密码
  password1Input: function (e) {
    var that = this;
    this.setData({
      password1: e.detail.value
    })
  },

  // 去登录页面
  goToLogin: function () {
    wx.reLaunch({
      url: "/pages/login/index",
    })
  },
 
  // 注册处理
  handleGetUserInfo: function(e) { 
    var that = this;
    if (this.data.username.length == 0 || this.data.password.length == 0) {
      wx.showToast({
        title: '账号或密码不能为空',
        icon: 'none',
        duration: 2000
      })
    } else if (this.data.password != this.data.password1) {
      wx.showToast({
        title: '两次密码输入不一致',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.request({
        url: 'http://127.0.0.1:8080/system/user/register', // 仅为示例，并非真实的接口地址
        method: 'post',
        data: {
          userName: that.data.username,
          password: that.data.password,
          nickName: that.data.username,
          status: "0",
          roleIds: [100]
        },
        header: {
          'content-type': 'application/json'  //这里注意POST请求content-type是小写，大写会报错  
        },  
        success(res) {
          if (res.data.code == 200) {
            wx.showToast({
              title: "注册成功"
            })
            setTimeout(function(){
              wx.reLaunch({
                url: "/pages/login/index"
              })
            }, 1000)
          } else {
            wx.showToast({
              title: "注册失败",
              icon: 'none'
            })
          }
        }
      })
    }
  }
})